﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using Entities;
using BUS;
using Utility;

public partial class A_LetsDeal_DoanhNghiep_sodutaikhoan : System.Web.UI.Page
{
    protected float loinhuan = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username_dn"] != null)
            {
                load_data();
            }
            else
            {
                Response.Redirect("~/A_LetsDeal-DoanhNghiep/Default.aspx");
            }
        }
    }

    protected void load_data()
    {
        Criteria c = new Criteria();
        int total = 0;
        string doanhnghiep = Session["username_dn"].ToString();
        Tbl_DoanhNghiep_Entities dn = Tbl_DoanhNghiep_BUS.GetByName(doanhnghiep);
        c.add(Condition.AND, Tbl_SanPham_Columns.DoanhNghiep, Condition.EQUAL,dn.STT);
        Tbl_SanPham_Collection spCollection =
            Tbl_SanPham_BUS.Get(c.Criter, 0, 0,
            Tbl_SanPham_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = spCollection;
        Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                  e.Item.ItemType == ListItemType.Item)
        {
            Tbl_SanPham_Entities sp = (Tbl_SanPham_Entities)e.Item.DataItem;
            Tbl_CTDH_Collection ctdh = Tbl_CTDH_BUS.GetByIDSP(sp.STT);
            for (int i = 0; i < ctdh.Count; i++)
            {
                Tbl_DonHang_Entities dh = Tbl_DonHang_BUS.GetByID(ctdh[i].DonHang);

                HyperLink hyp_tensp = (HyperLink)e.Item.FindControl("hyp_tensp");
                Literal lit_thuvao = (Literal)e.Item.FindControl("lit_thuvao");
                Literal lit_huehong = (Literal)e.Item.FindControl("lit_huehong");
                Literal lit_loinhuan = (Literal)e.Item.FindControl("lit_loinhuan");
                Literal lit_slb = (Literal)e.Item.FindControl("lit_slb");

                int soluongban = Tbl_CTDH_BUS.CountByIDSP(sp.STT);
                lit_slb.Text = soluongban + "/" + sp.SLBan.ToString();
                hyp_tensp.NavigateUrl = "#";
                if (sp.TenSP.Length >= 70)
                {
                    hyp_tensp.Text = sp.TenSP.Substring(0, 70) + "...";
                    hyp_tensp.ToolTip = sp.TenSP;
                }
                else
                {
                    hyp_tensp.Text = sp.TenSP;
                }
                hyp_tensp.NavigateUrl = "~/A_LetsDeal-DoanhNghiep/chitietsanpham.aspx?id=" + sp.STT.ToString();
                lit_thuvao.Text = String.Format("{0:0,0 đ}", soluongban * sp.GiaBan);
                lit_huehong.Text = String.Format("{0:0,0 đ}", (soluongban * sp.GiaBan) / 10);
                lit_loinhuan.Text = String.Format("{0:0,0 đ}", (soluongban * sp.GiaBan) - (soluongban * sp.GiaBan) / 10);
                loinhuan = loinhuan + ((soluongban * sp.GiaBan) - (soluongban * sp.GiaBan) / 10);
            }
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Literal lit_tongloinhuan = (Literal)e.Item.FindControl("lit_tongloinhuan");
            lit_tongloinhuan.Text = lit_tongloinhuan.Text + " " + String.Format("{0:0,0 đ}",loinhuan);
        }
    }

}
